﻿using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Service
{
    public class SuburlPermissionService : ServiceBase
    {
        public List<WeburlInfo> GetAccessUrl(string connectionString, int roleOrLevelId, AccountTypeValue accountType)
        {
            List<SqlParameter> paras = new List<SqlParameter>();

            paras.Add(new SqlParameter("@RoleOrLevel", roleOrLevelId));
            paras.Add(new SqlParameter("@AccountType", (int)accountType));

            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM t_weburl AS tw  ");
            builder.Append("WHERE tw.Id IN (SELECT twp.UrlId FROM t_weburl_permission AS twp WHERE twp.RoleOrLevelId = @RoleOrLevel AND twp.AccountType=@AccountType) ");

            return DbOperator.GetEntities<WeburlInfo>(connectionString, builder.ToString(), paras.ToArray());
        }

        public bool Update(string connectionString, int roleOrLevelId, AccountTypeValue accountType, params int[] updateIds)
        {
            List<int> deleteId = new List<int>();
            List<int> insertId = new List<int>();
            IEnumerable<int> old = GetExists(connectionString, roleOrLevelId, accountType);
            if (old != null)
            {
                deleteId.AddRange(old.Where(t => !updateIds.Contains(t)));
                insertId.AddRange(updateIds.Where(t => !old.Contains(t)));
            }
            else
            {
                insertId.AddRange(updateIds);
            }

            List<SqlParameter> paras = new List<SqlParameter>();

            paras.Add(new SqlParameter("@RoleOrLevel", roleOrLevelId));
            paras.Add(new SqlParameter("@AccountType", (int)accountType));

            StringBuilder builder = new StringBuilder();

            if (deleteId.Count > 0)
            {
                builder.AppendFormat("DELETE FROM t_weburl_permission WHERE RoleOrLevelId=@RoleOrLevel AND AccountType=@AccountType AND UrlId IN ({0}) ", string.Join(",", deleteId.ToArray()));
                //paras.Add(new SqlParameter("DeleteId", string.Join(",", deleteId.ToArray())));
            }

            for (var i = 0; i < insertId.Count; i++)
            {
                builder.AppendFormat("INSERT INTO t_weburl_permission(UrlId, RoleOrLevelId, AccountType) VALUES(@InsertUrlId_{0}, @RoleOrLevel, @AccountType) ", i);
                paras.Add(new SqlParameter("@InsertUrlId_" + i, insertId[i]));
            }

            return DbOperator.UpdateEntity(connectionString, builder.ToString(), paras.ToArray());
        }

        private IEnumerable<int> GetExists(string connectionString, int roleOrLevelId, AccountTypeValue accountType)
        {
            string queryString = "SELECT twp.UrlId FROM t_weburl_permission AS twp WHERE twp.RoleOrLevelId = @RoleOrLevel AND twp.AccountType=@AccountType";

            List<SqlParameter> paras = new List<SqlParameter>();

            paras.Add(new SqlParameter("@RoleOrLevel", roleOrLevelId));
            paras.Add(new SqlParameter("@AccountType", (int)accountType));

            DataTable table = DbOperator.GetDataTable(connectionString, queryString, paras.ToArray());

            if (table == null || table.Rows.Count == 0)
            {
                return null;
            }

            return table.AsEnumerable().Select(t => Convert.ToInt32(t[0]));
        }


    }
}
